#include "wasm_utils.h"
#include <time.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <unistd.h>
#include <stdarg.h>

void debug(const char *format, ...)
{
    char szBuffer[1024] = {0};
    char szTime[32] = {0};
    char *p = NULL;
    int prefixLength = 0;
    const char *tag = "Core";
    struct tm tmTime;
    struct timeb tb;

    ftime(&tb);
    localtime_r(&tb.time, &tmTime);

    if (1)
    {
        int tmYear = tmTime.tm_year + 1900;
        int tmMon = tmTime.tm_mon + 1;
        int tmMday = tmTime.tm_mday;
        int tmHour = tmTime.tm_hour;
        int tmMin = tmTime.tm_min;
        int tmSec = tmTime.tm_sec;
        int tmMillisec = tb.millitm;
        sprintf(szTime, "%d-%d-%d %d:%d:%d.%d", tmYear, tmMon, tmMday, tmHour, tmMin, tmSec, tmMillisec);
    }

    prefixLength = sprintf(szBuffer, "[%s][%s][DT] ", szTime, tag);
    p = szBuffer + prefixLength;

    if (1)
    {
        va_list ap;
        va_start(ap, format);
        vsnprintf(p, 1024 - prefixLength, format, ap);
        va_end(ap);
    }

    printf("%s\n", szBuffer);
}

//-------------------------------------------
void sync_idbfs()
{
#if defined(__EMSCRIPTEN__)

    EM_ASM(
        FS.syncfs(function(err){}););
#endif
}

void init_idbfs_data()
{
#if defined(__EMSCRIPTEN__)

    EM_ASM(
        FS.mkdir('/data');
        FS.mount(IDBFS, {}, '/data'););
#endif
}

//-------------------------------------------
